// 
// Entify project
//  
// Author:
//       Lauri Taimila <lauri@taimila.com>
// 
// Copyright (c) 2010 Lauri Taimila
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;

namespace MonoDevelop.EntifyEditor
{
	/// <summary>
	/// Widget for editing entity details.
	/// </summary>
	public partial class EntityDetailsWidget : Gtk.Bin
	{
		/// <summary>
		/// Event indicates that entity details has been changed from the editor.
		/// </summary>
		public delegate void EntityDetailsChanged();
    	public event EntityDetailsChanged DetailsChanged;

		/// <summary>
		/// Entity which details are modified with this widget.
		/// </summary>
		private Entity model;

		/// <summary>
		/// Sets entity model to view. Updates view to represent
		/// current model.
		/// </summary>
		public Entity Model
		{
			set
			{
				this.model = value;
				this.UpdateView();
			}
		}

		/// <summary>
		/// Creates a new Entity details widget.
		/// </summary>
		public EntityDetailsWidget()
		{
			this.Build();
			this.model = null;
			this.UpdateView();
		}

		public void FocusToEntityNameEntry()
		{
			this.EntityNameEntry.HasFocus = true;
		}

		/// <summary>
		/// Updates UI components to represent values of the current model.
		/// </summary>
		private void UpdateView()
		{
			if(this.model == null)
			{
				this.EntityNameEntry.Text = "";
				this.EntityDescriptionEntry.Text = "";
				this.EntityVersionSpin.Value = 1.0;
				this.EntityDetails.Sensitive = false;
				this.EntityColorButton.Color = new Gdk.Color(0,0,0);
			}
			else
			{
				this.EntityNameEntry.Text = this.model.Name;
				this.EntityDescriptionEntry.Text = this.model.Description;
				this.EntityVersionSpin.Value = this.model.Version;
				this.EntityDetails.Sensitive = true;

				this.EntityColorButton.Color = new Gdk.Color(
				                                             (byte) Math.Floor(this.model.RGB[0] / 255.0),
				                                             (byte) Math.Floor(this.model.RGB[1] / 255.0),
				                                             (byte) Math.Floor(this.model.RGB[2] / 255.0)
				                                            );
			}
		}

		/// <summary>
		/// Called when entity name has been changed. Updates the model.
		/// </summary>
		protected virtual void OnEntityNameEntryChanged (object sender, System.EventArgs e)
		{
			if(this.model != null)
			{
				this.model.Name = this.EntityNameEntry.Text.Trim();
				this.DetailsChanged();
			}
		}

		/// <summary>
		/// Called when entity version number has been changed. Updates the model.
		/// </summary>
		protected virtual void OnEntityVersionSpinChanged (object sender, System.EventArgs e)
		{
			if(this.model != null)
			{
				this.model.Version = this.EntityVersionSpin.Value;
				this.DetailsChanged();
			}
		}

		/// <summary>
		/// Called when entity description has been changed. Updates the model.
		/// </summary>
		protected virtual void OnEntityDescriptionEntryChanged (object sender, System.EventArgs e)
		{
			if(this.model != null)
			{
				this.model.Description = this.EntityDescriptionEntry.Text.Trim();
				this.DetailsChanged();
			}
		}

		/// <summary>
		/// Called when new color has been selected for current entity.
		/// </summary>
		protected virtual void OnEntityColorButtonColorSet (object sender, System.EventArgs e)
		{
			if(this.model != null)
			{
				Gdk.Color c = this.EntityColorButton.Color;
				this.model.RGB = new List<int>() { c.Red, c.Green, c.Blue };
				this.DetailsChanged();
			}
		}
	}
}
